<?php
class Debug{
    static private $startTime;
    static private $stopTime;
    static private $info=array();
    static private $sqls=array();
    static private $includefile=array();
    
    static private $msg=array(
            E_WARNING=>'运行时警告',
            E_NOTICE=>'运行时提醒',
            E_STRICT=>'编码标准化警告',
            E_USER_ERROR=>'自定义错误',
            E_USER_WARNING=>'自定义警告',
            E_USER_NOTICE=>'自定义提醒',
            'Unknown'=>'未知错误 '
    );
    
    
    
    
    static public function start(){
        self::$startTime=microtime(true);
    }
    
    static public function stop(){
        self::$stopTime=microtime(true);
    }
    
    static public function catcher($errno,$errstr,$errfile,$errline){
        if (!isset(self::$msg[$errno]))
            $errno='Unknown';
        if (self::$msg[$errno]==E_NOTICE || self::$msg[$errno]==E_USER_NOTICE)
            $color='#000088';
        else
            $color='red';
        $mess='<font color="'.$color.'">';
        $mess.='<b>'.self::$msg[$errno]."</b>[在文件{$errfile}中,第{$errline}行]:";
        $mess.=$errstr;
        $mess.='</font>';
        self::addMsg($mess);
    }
    
    static public function addMsg($msg,$type=0){
        if (defined('APP_DEBUG') && APP_DEBUG==true){
            switch ($type){
                case 0:
                  self::$info[]=$msg;
                  break;
                case 1:
                  self::$includefile[]=$msg;
                  break;
                case 2:
                  self::$sqls[]=$msg;
                  break;
            }
        }
    }
    
    static public function message(){
        echo '<div style="float:left;clear:both;text-align:left;font-size:11px;color:#888;width:95%;background:#F5F5F5;border:1px dotted #778855;z-index:100;margin:10px;padding:10px;">';
        echo '<div style="float:left;width:100%;"><span style="float:left;width:200px;"><b>运行信息</b>( <font color="red">'.self::spent().'</font>秒 ):</span><span onclick="this.parentNode.parentNode.style.display=\'none\'" style="cursor:pointer;float:right;width:35px;background:#500;border:1px solid #555;color:white;">关闭X</span></div><br />';
        echo '<ul style="margin:0px;padding:0px 10px 0px 10px;list-style:none;">';
        if (count(self::$includefile)>0){
            echo '[自动包含]';
            foreach (self::$includefile as $file)
                echo '<li>&nbsp;&nbsp;&nbsp;&nbsp;'.$file.'</li>';
        }
        
       if (count(self::$info) >0){
           echo '<br />[系统信息]';
           foreach (self::$info as $info)
               echo '<li>&nbsp;&nbsp;&nbsp;&nbsp;'.$info.'</li>';
       }
       
       if (count(self::$sqls) > 0){
          echo '<br />[SQL语句]';
          foreach (self::$sqls as $sql)
              echo '<li>&nbsp;&nbsp;&nbsp;&nbsp;'.$sql.'</li>';
          echo '</ul>';
          echo '</div>';
       }
    }
    
    static public function spent(){
        return round((self::$stopTime - self::$startTime),4);
    }









}